using System;
using System.Threading;
using System.Threading.Tasks;

namespace MatrixFramework.Core.MultiTenancy;

/// <summary>
/// 租户数据库迁移服务接口
/// </summary>
public interface ITenantDatabaseMigrationService
{
    /// <summary>
    /// 为租户创建数据库
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>任务</returns>
    Task CreateTenantDatabaseAsync(Guid tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 为租户执行数据库迁移
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>任务</returns>
    Task MigrateTenantDatabaseAsync(Guid tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除租户数据库
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>任务</returns>
    Task DropTenantDatabaseAsync(Guid tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 检查租户数据库是否存在
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>是否存在</returns>
    Task<bool> TenantDatabaseExistsAsync(Guid tenantId, CancellationToken cancellationToken = default);

    /// <summary>
    /// 为所有租户执行数据库迁移
    /// </summary>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>任务</returns>
    Task MigrateAllTenantDatabasesAsync(CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取租户数据库连接测试结果
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="cancellationToken">取消令牌</param>
    /// <returns>测试结果</returns>
    Task<TenantDatabaseTestResult> TestTenantDatabaseConnectionAsync(Guid tenantId, CancellationToken cancellationToken = default);
}

/// <summary>
/// 租户数据库测试结果
/// </summary>
public class TenantDatabaseTestResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    public string ErrorMessage { get; set; } = string.Empty;

    /// <summary>
    /// 连接时间（毫秒）
    /// </summary>
    public long ConnectionTimeMs { get; set; }

    /// <summary>
    /// 数据库版本
    /// </summary>
    public string DatabaseVersion { get; set; } = string.Empty;

    /// <summary>
    /// 是否已应用最新迁移
    /// </summary>
    public bool IsLatestMigrationApplied { get; set; }

    /// <summary>
    /// 当前应用的迁移版本
    /// </summary>
    public string CurrentMigrationVersion { get; set; } = string.Empty;

    /// <summary>
    /// 待应用的迁移数量
    /// </summary>
    public int PendingMigrationsCount { get; set; }
}